package sy.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import sy.pageModel.DataGrid;
import sy.pageModel.Json;
import sy.pageModel.VehicleInfo;
import sy.service.VehicleInfoServiceI;

@Controller
@RequestMapping("/vehicleInfoController")
public class VehicleInfoController {

	private VehicleInfoServiceI vehicleInfoService;

	public VehicleInfoServiceI getVehicleInfoService() {
		return vehicleInfoService;
	}

	@Autowired
	public void setVehicleInfoService(VehicleInfoServiceI vehicleInfoService) {
		this.vehicleInfoService = vehicleInfoService;
	}

	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	@RequestMapping("/reg")
	@ResponseBody
	public Json reg(VehicleInfo vehicleInfo) {
		Json j = new Json();
		try {
			VehicleInfo vehicle = vehicleInfoService.save(vehicleInfo);
			j.setSuccess(true);
			j.setMsg("注册成功！");
			j.setObj(vehicle);
		} catch (Exception e) {
			j.setMsg("注册失败！用户名已存在！");
		}
		return j;
	}

	@RequestMapping("/combogrid")
	@ResponseBody
	public DataGrid combogrid(VehicleInfo vehicleInfo) {
		System.out.println("vehicleInfo***combogrid");
		return vehicleInfoService.datagrid(vehicleInfo);
	}

	@RequestMapping("/combobox")
	@ResponseBody
	public List<VehicleInfo> combobox(VehicleInfo vehicleInfo) {
		return vehicleInfoService.combobox(vehicleInfo);
	}

	@RequestMapping("/datagrid")
	@ResponseBody
	public DataGrid datagrid(VehicleInfo vehicleInfo) {
		System.out.println("测试datagrid**vehicleInfo*");
		return vehicleInfoService.datagrid(vehicleInfo);
	}

	@RequestMapping("/remove")
	@ResponseBody
	public Json remove(VehicleInfo vehicleInfo) {
		System.out.println("/remove******vehicleInfo");
		vehicleInfoService.remove(vehicleInfo.getIds());
		Json j = new Json();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		return j;
	}

	@RequestMapping("/add")
	@ResponseBody
	public Json add(VehicleInfo vehicleInfo) {
		Json j = new Json();
		try {
			System.out.println("/add***vehicleInfo*"+vehicleInfo.getDescription());
			VehicleInfo vehicle = vehicleInfoService.save(vehicleInfo);
			System.out.println(vehicle.getDescription());
			j.setSuccess(true);
			j.setMsg("添加成功！");
			j.setObj(vehicle);
		} catch (Exception e) {
			j.setMsg("添加失败！车辆已存在！");
		}
		return j;
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(VehicleInfo vehicleInfo) {
		System.out.println("edit*000****vehicleInfo****");
		Json j = new Json();
		try {
			VehicleInfo vehicle = vehicleInfoService.edit(vehicleInfo);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
			j.setObj(vehicle);
		} catch (Exception e) {
			j.setMsg("编辑失败！用户名已存在！");
		}
		return j;
	}
}
